package com.simba.server.commands.impl.record;

import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;
import com.simba.server.base.CommonUtil;
import com.simba.server.components.data.SortInfo;

public class RecordPacketOffCmd implements ICommand
{
	public static final String NAME = "RecordPacketOffCmd";
	private final SortInfo sortInfo;
	private final String sql = "insert into tt_packet_off_info("
			+ "barcode,sorterId,plcId,serverSlots,droppedSlot,sortingId,boxCode,turnNumber,obrNo,"
			+ "errorCodeSVR,errorCodePLC,packetOnTime,packetOffTime,uploadFlag, uploadNum, slotDistrict,"
			+ "planId, roadSlot, roadSlotName, syncFlag, weight,length,width,height,volume"
			+ ") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

	public RecordPacketOffCmd(SortInfo sortInfo)
	{
		this.sortInfo = sortInfo;
	}

	@Override
	public String getName()
	{
		return RecordPacketOffCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		boolean ret = false;
		if (!(params instanceof DbOperator))
		{
			return ret;
		}
		try
		{

			DbOperator dbOperator = (DbOperator) params;
			dbOperator.prepareStatement(this.sql);

			dbOperator.setString(1,  CommonUtil.doNullData(this.sortInfo.getBarcode(), "none"));
			dbOperator.setString(2,  CommonUtil.doNullData(this.sortInfo.getSorterId(), "none"));
			dbOperator.setString(3,  CommonUtil.doNullData(this.sortInfo.getPlcId(), "none"));
			dbOperator.setString(4,  CommonUtil.doNullData(this.createServerSlots(), "none"));
			dbOperator.setInt(5,  this.sortInfo.getDroppedSlotId());
			dbOperator.setString(6,  CommonUtil.doNullData(this.createSortingId(), "none"));
			dbOperator.setString(7,  CommonUtil.doNullData(this.sortInfo.getBoxCode(), "none"));
			dbOperator.setInt(8, this.sortInfo.getTurnNumber());
			dbOperator.setInt(9, this.sortInfo.getObr());
			dbOperator.setInt(10, this.sortInfo.getErrorCodeSVR());
			dbOperator.setInt(11, this.sortInfo.getErrorCodePLC());
			dbOperator.setString(12, CommonUtil.doNullData(this.sortInfo.getTimePacketOn(), "none"));
			dbOperator.setString(13, CommonUtil.doNullData(this.sortInfo.getTimePacketOff(), "none"));
			dbOperator.setInt(14, this.sortInfo.getUploadFlag());
			dbOperator.setInt(15, this.sortInfo.getUploadNum());
			dbOperator.setInt(16, this.sortInfo.getSlotDistrict());
			dbOperator.setString(17,  CommonUtil.doNullData(this.sortInfo.getPlanId(), "none"));
			dbOperator.setString(18,  CommonUtil.doNullData(this.sortInfo.getRoadSlot(), "none"));
			dbOperator.setString(19,  CommonUtil.doNullData(this.sortInfo.getRoadSlotName(), "none"));
			dbOperator.setInt(20,  0);
			dbOperator.setString(21,  CommonUtil.doNullData(this.sortInfo.getWeight()==null?this.sortInfo.getChargeWeight():this.sortInfo.getWeight(), "0"));
			dbOperator.setString(22,  CommonUtil.doNullData(this.sortInfo.getLength(), "0"));
			dbOperator.setString(23,  CommonUtil.doNullData(this.sortInfo.getWidth(), "0"));
			dbOperator.setString(24,  CommonUtil.doNullData(this.sortInfo.getHeight(), "0"));
			dbOperator.setString(25,  CommonUtil.doNullData(this.sortInfo.getVolume(), "0"));

			int res = dbOperator.executeUpdate();

			if (res > 0)
			{
				ret = true;
			}
			else
			{
				ret = false;
			}

		}
		catch (Exception ex)
		{
			ret = false;
		}
		return ret;
	}

	private String createServerSlots()
	{
		String serverSlots = "none";
		try
		{
			StringBuilder slotsContent = new StringBuilder();
			int slotNum = this.sortInfo.getSlotsList().size();
			for (int i = 0; i < slotNum; i++)
			{
				if (i != slotNum - 1)
				{
					slotsContent.append(String.valueOf(this.sortInfo.getSlotsList().get(i)));
					slotsContent.append(",");
				}
				else
				{
					slotsContent.append(String.valueOf(this.sortInfo.getSlotsList().get(i)));
				}
			}
			serverSlots = slotsContent.toString();
			if (serverSlots.equals(""))
			{
				serverSlots = "none";
			}
		}
		catch (Exception e)
		{
			serverSlots = "ex-server-slots";
		}

		return serverSlots;
	}

	private String createSortingId()
	{
		String sortingId = "none";
		try
		{
			sortingId = this.sortInfo.getSortingId();
			if ((sortingId == null) || sortingId.equals(""))
			{
				sortingId = "none";
			}

			if (sortingId.length() > 16)
			{
				sortingId = sortingId.substring(0, 16);
			}
		}
		catch (Exception e)
		{
			sortingId = "ex-sorting-id";
		}

		return sortingId;
	}
}
